package entity

import (
	"QImages/database"
	"github.com/jinzhu/gorm"
)

type Collection struct {
	gorm.Model
	UserID  uint
	ImageID uint
}

func init() {
	database.DB.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").AutoMigrate(&Collection{})
}

// Insert 添加收藏信息
func (c *Collection) Insert() (id uint, err error) {
	result := database.DB.Create(&c)
	id = c.ID
	if result.Error != nil {
		err = result.Error
		return
	}
	return
}

func FindByImageID(imageId uint) (res []uint, err error) {
	var cs []Collection
	if err = database.DB.Select("distinct user_id").Where("image_id = ?", imageId).Find(&cs).Error; err != nil {
		return
	}
	for _, c := range cs {
		res = append(res, c.UserID)
	}
	return
}


